import { useEffect, useState } from 'react';
import { StyleSheet, View, Text, Button } from 'react-native';
import * as Brightness from 'expo-brightness';

export default function App() {
    const [currentBrightness, setCurrentBrightness] = useState(0);

    // 获取当前亮度
    const getBrightness = async () => {
        const brightness = await Brightness.getSystemBrightnessAsync();
        setCurrentBrightness(brightness);
        console.log('当前亮度：', brightness); // 0-1 之间的值
    };

    // 设置亮度
    const setBrightness = async (value: number) => {
        await Brightness.setSystemBrightnessAsync(value);
        await getBrightness();
    };

    useEffect(() => {
        (async () => {
            const { status } = await Brightness.requestPermissionsAsync();
            if (status === 'granted') {
                await getBrightness();
            }
        })();
    }, []);

    return (
        <View style={styles.container}>
            <Text>当前亮度: {Math.round(currentBrightness * 100)}%</Text>
            <View style={styles.buttonContainer}>
                <Button title="调暗" onPress={() => setBrightness(0.2)} />
                <Button title="正常" onPress={() => setBrightness(0.5)} />
                <Button title="调亮" onPress={() => setBrightness(1)} />
            </View>
        </View>
    );
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#fff',
        alignItems: 'center',
        justifyContent: 'center',
        gap: 20,
    },
    buttonContainer: {
        flexDirection: 'row',
        gap: 10,
    }
});
